package cn.coder.toolkit;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.StringJoiner;

/**
 * @author com.mrathena on 2019/5/27 11:19
 */
public final class IpKit {

	private IpKit() {}

	public static final String MINUS = "-";

	public static String getLocalIp() throws UnknownHostException {
		return InetAddress.getLocalHost().getHostAddress();
	}

	/**
	 * 获取本机Mac
	 */
	public static String getLocalMac() throws UnknownHostException, SocketException {
		StringJoiner joiner = new StringJoiner(MINUS);
		InetAddress localHost = InetAddress.getLocalHost();
		byte[] macArray = NetworkInterface.getByInetAddress(localHost).getHardwareAddress();
		for (byte macPart : macArray) {
			String temp = Integer.toHexString(macPart & 0xFF);
			joiner.add(temp.length() == 1 ? "0" + temp : temp);
		}
		return joiner.toString();
	}

}
